{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9eea20b7",
   "metadata": {},
   "source": [
    "## 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "23fe5ff6",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"\\n解题思路：\\n1.首先将文本内容中的干扰项去除，去除','和'.'\\n2.将文本中的单词分割成,并形成一个字符串列表beginList\\n3.循环查看每个单词，并判断这个单词是否在新列表（countList）中，不在就添加到新列表中，这样可以得到没有重复单词的List\\n4.遍历新列表中的每个单词，将不重复的单词用在beginList计数中，计算beginList中每个单词出现的次数\\n5.打印输出结果\\n\\n遇到的问题：\\nint类型不能和字符串类型进行拼接，要将int强转成字符串类型才可拼接\\n\""
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "解题思路：\n",
    "1.首先将文本内容中的干扰项去除，去除','和'.'\n",
    "2.将文本中的单词分割成,并形成一个字符串列表beginList\n",
    "3.循环查看每个单词，并判断这个单词是否在新列表（countList）中，不在就添加到新列表中，这样可以得到没有重复单词的List\n",
    "4.遍历新列表中的每个单词，将不重复的单词用在beginList计数中，计算beginList中每个单词出现的次数\n",
    "5.打印输出结果\n",
    "\n",
    "遇到的问题：\n",
    "int类型不能和字符串类型进行拼接，要将int强转成字符串类型才可拼接\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d95f1828",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['One', 'is', 'always', 'on', 'a', 'strange', 'road', 'watching', 'scenery', 'and', 'listening', 'to', 'music', 'Then', 'one', 'day', 'you', 'will', 'find', 'that', 'the', 'things', 'try', 'hard', 'forget', 'are', 'already', 'gone']\n",
      "文本中出现One的次数为：1\n",
      "文本中出现is的次数为：1\n",
      "文本中出现always的次数为：1\n",
      "文本中出现on的次数为：1\n",
      "文本中出现a的次数为：1\n",
      "文本中出现strange的次数为：3\n",
      "文本中出现road的次数为：1\n",
      "文本中出现watching的次数为：1\n",
      "文本中出现scenery的次数为：1\n",
      "文本中出现and的次数为：1\n",
      "文本中出现listening的次数为：1\n",
      "文本中出现to的次数为：2\n",
      "文本中出现music的次数为：1\n",
      "文本中出现Then的次数为：1\n",
      "文本中出现one的次数为：1\n",
      "文本中出现day的次数为：1\n",
      "文本中出现you的次数为：2\n",
      "文本中出现will的次数为：1\n",
      "文本中出现find的次数为：1\n",
      "文本中出现that的次数为：1\n",
      "文本中出现the的次数为：1\n",
      "文本中出现things的次数为：1\n",
      "文本中出现try的次数为：1\n",
      "文本中出现hard的次数为：1\n",
      "文本中出现forget的次数为：1\n",
      "文本中出现are的次数为：1\n",
      "文本中出现already的次数为：1\n",
      "文本中出现gone的次数为：1\n"
     ]
    }
   ],
   "source": [
    "text = \"One is always on a strange road, watching strange scenery and listening to strange music. Then one day, you will find that the things you try hard to forget are already gone.  \"\n",
    "text = text.replace(', ',' ')\n",
    "text = text.replace('. ',' ')\n",
    "\n",
    "beginList = text.split()\n",
    "\n",
    "countList = []\n",
    "for item in beginList:\n",
    "    if item not in countList:\n",
    "        countList.append(item)\n",
    "print(countList)\n",
    "\n",
    "for item in countList:\n",
    "    shuLiang = beginList.count(item)\n",
    "    print(\"文本中出现\"+item+\"的次数为：\"+str(shuLiang))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1669fb84",
   "metadata": {},
   "source": [
    "## 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2bae0f59",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "解题思路：\n",
    "1.创建一个空列表\n",
    "2.用三层循环遍历得到1，2，3，4可以组成的三位数：\n",
    "  第一层循环随机得到4个数中的任意一个\n",
    "  第二层循环随机得到另外三个数\n",
    "  第三层循环随机得到未取的另外两个数\n",
    "3.将三个数写成数学形式（i*100+j*10+k），生成后依次添加到空数组中\n",
    "4.最后用len(nums)方法得到数组中元素个数\n",
    "5.并打印结果\n",
    "\n",
    "遇到的问题：跳出循环的选择，最初选用的是break，直接跳出循环，造成了数据缺失。\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9088d253",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 1、2、3、4 能组成24个互不相同且无重复数字的三位数。它们是：\n",
      "[123, 124, 132, 134, 142, 143, 213, 214, 231, 234, 241, 243, 312, 314, 321, 324, 341, 342, 412, 413, 421, 423, 431, 432]\n"
     ]
    }
   ],
   "source": [
    "nums = []\n",
    "for i in range(1,5):\n",
    "    for j in range(1,5):\n",
    "        if j != i:\n",
    "            for k in range(1,5):\n",
    "                if k != i and k != j:\n",
    "                    num = i*100+j*10+k\n",
    "                    nums.append(num)\n",
    "                else:\n",
    "                    continue\n",
    "        else:\n",
    "            continue\n",
    "\n",
    "print(\" 1、2、3、4 能组成\"+str(len(nums))+\"个互不相同且无重复数字的三位数。它们是：\")\n",
    "print(nums)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5b8772c",
   "metadata": {},
   "source": [
    "## 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3afaecc3",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "解题思路：\n",
    "1.input()输入月利润，下面要对其正确性进行判断（>0）\n",
    "2.if...elif...else 判断月利润所在范围，并执行奖金数\n",
    "3.打印结果\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3dca041e",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入月利润(单位/万元)： 20\n",
      "应发的奖金总数为：1.75万元\n"
     ]
    }
   ],
   "source": [
    "MonthProfit = float(input(\"请输入月利润(单位/万元)： \"))\n",
    "if MonthProfit < 0:\n",
    "    print(\"月利润输入有误，请重新输入！\")\n",
    "elif MonthProfit <= 10:\n",
    "    prize = MonthProfit*0.1\n",
    "elif MonthProfit <= 20:\n",
    "    prize = 10*0.1+(MonthProfit-10)*0.075\n",
    "elif MonthProfit <= 40:\n",
    "    prize = 10*0.1+10*0.075+(MonthProfit-20)*0.05\n",
    "elif MonthProfit <= 60:\n",
    "    prize = 10*0.1+10*0.075+20*0.05+(MonthProfit-40)*0.03\n",
    "elif MonthProfit <= 100:\n",
    "    prize = 10*0.1+10*0.075+20*0.05+20*0.03+(MonthProfit-60)*0.015\n",
    "else:\n",
    "    prize = 10*0.1+10*0.075+20*0.05+20*0.03+40*0.015+(MonthProfit-100)*0.01\n",
    "print(\"应发的奖金总数为：\"+str(prize)+\"万元\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15d9feaa",
   "metadata": {},
   "source": [
    "## 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1708df82",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "解题思路：\n",
    "1.两层循环输出乘法表\n",
    "  第一层循环，循环数字范围为1-9，确定第一个数字\n",
    "  第二层循环，循环范围在1-i(第一层循环确定)，确定第二个数字\n",
    "2.打印输出\n",
    "\n",
    "遇到的问题：该题数学表达式对应的打印语句规范不清楚，打印时出现错误\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2e19614f",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "九九乘法表:\n",
      "1×1=1\t\n",
      "1×2=2\t2×2=4\t\n",
      "1×3=3\t2×3=6\t3×3=9\t\n",
      "1×4=4\t2×4=8\t3×4=12\t4×4=16\t\n",
      "1×5=5\t2×5=10\t3×5=15\t4×5=20\t5×5=25\t\n",
      "1×6=6\t2×6=12\t3×6=18\t4×6=24\t5×6=30\t6×6=36\t\n",
      "1×7=7\t2×7=14\t3×7=21\t4×7=28\t5×7=35\t6×7=42\t7×7=49\t\n",
      "1×8=8\t2×8=16\t3×8=24\t4×8=32\t5×8=40\t6×8=48\t7×8=56\t8×8=64\t\n",
      "1×9=9\t2×9=18\t3×9=27\t4×9=36\t5×9=45\t6×9=54\t7×9=63\t8×9=72\t9×9=81\t\n"
     ]
    }
   ],
   "source": [
    "print(\"九九乘法表:\")\n",
    "for i in range(1,10):\n",
    "    for j in range(1,i+1):\n",
    "        print(\"%d×%d=%d\"%(j,i,i*j),end='\\t')\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "927636de",
   "metadata": {},
   "source": [
    "## 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "af00221d",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "解题思路：\n",
    "1.根据表达式可看出，需要从2每次自增，且自增的数要乘以（-1）自增数幂\n",
    "  即可直接根据循环次数，写出代码\n",
    "2.打印结果\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0002da14",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2-3+4-5+6...+100的和为：51\n"
     ]
    }
   ],
   "source": [
    "i = 2\n",
    "sum = 0\n",
    "while i <= 100:\n",
    "    sum += i*((-1)**i)\n",
    "    i += 1\n",
    "print(\"2-3+4-5+6...+100的和为：\"+str(sum))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8e526c3",
   "metadata": {},
   "source": [
    "## 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "667d4211",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "解法一：\n",
    "循环n次,每次找到列表中的最大数，然后将其从列表中移除，创建一个新列表，将其放在新列表的第一个\n",
    "然后继续寻找列表中的最大值，移除后放在新列表中，即可排序\n",
    "遇到的问题：\n",
    "temp为找到最大数，但列表的移除方法无法移除变量，只能移除值，因此无法移除。\n",
    "\n",
    "while i < len(nums):\n",
    "    for item in nums:\n",
    "        if temp <= item:\n",
    "            temp = item\n",
    "            nums.remove()\n",
    "            nums.insert(0,temp)\n",
    "    nums.pop(0)\n",
    "    newNums.append(nums[i])\n",
    "print(newNums)\n",
    "'''\n",
    "\n",
    "'''\n",
    "解法二\n",
    "解题思路：\n",
    "1.冒泡排序，两层循环\n",
    "  第一层，确定需要循环的总次数，依次循环。循环n次\n",
    "  第二层，从列表左边开始两两对比，大的放右边，这样每次循环保证最小的往后放（即可排序）\n",
    "2.循环完毕，打印列表\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ed48662d",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[34, 10, 9, 9, 8, 5, 5, 4, 2, 2, 1, 0]\n"
     ]
    }
   ],
   "source": [
    "nums = [1, 10, 4, 2, 9, 2, 34, 5, 9, 8, 5, 0]\n",
    "n = len(nums)\n",
    "for i in range(n):\n",
    "    for j in range(1, n - i):\n",
    "        if nums[j - 1] < nums[j]:\n",
    "            temp = nums[j-1]\n",
    "            nums[j-1] = nums[j]\n",
    "            nums[j] = temp\n",
    "print(nums)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c0bcbff",
   "metadata": {},
   "source": [
    "## 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04b7afba",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "解题思路（参考网上已理解）\n",
    "1.从矩阵可以看出：每行最左边的元素都是该行最小的，同时它的下方和右方又都比他大。\n",
    "因此：矩阵左下角的元素，所有比他大的元素都在他右侧（所在列右侧），所有比他小的元素都在他上侧（所在行上侧）。\n",
    "2.所以可以用最后一行最左边的元素作为起始索引（i，j）。\n",
    "若是索引值大于target，说明target可能再索引所在行的上方，i-=1；若是索引值小于target，说明target可能在索引所在列的右方，j+=1；若是恰好是该值，则输出。\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7e04f65",
   "metadata": {},
   "outputs": [],
   "source": [
    "matrix = [[1, 4, 7, 11, 15],[2, 5, 8, 12, 19],[3, 6, 9, 16, 22],[10, 13, 14, 17, 24],[18, 21, 23, 26, 30]]\n",
    "target = input(\"请输入目标值： \")\n",
    "class Solution:\n",
    "    def searchMatrix(self, matrix,target):\n",
    "        if not matrix:\n",
    "            return False\n",
    "        rol = len(matrix)\n",
    "        col = len(matrix[0])\n",
    "        i = rol - 1\n",
    "        j = 0\n",
    "        judge = False\n",
    "        while (i >= 0) or (j < col):\n",
    "            if i < 0 or j > (col - 1):\n",
    "                break\n",
    "            if matrix[i][j] > target:\n",
    "                i = i -1\n",
    "            elif matrix[i][j] > target:\n",
    "                j = j+1\n",
    "            elif matrix[i][j] == target:\n",
    "                judge = True\n",
    "                break\n",
    "        return judge"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4224663b",
   "metadata": {},
   "source": [
    "## 8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a132e9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 激活码要具有唯一性，还要有一定的复杂性和无规律性\n",
    "# 假设激活码由10个字符组成：字符随机包括大小写字母和数字\n",
    "'''\n",
    "解法一：\n",
    "随机生成不重复的10位数字当验证码\n",
    "1.建立空列表pwds\n",
    "2.双层循环生成随机激活码\n",
    "  第一层，循环200次，生成200个随机数\n",
    "  第二层，循环十次，每次随机产生一个数，并进行字符串拼接，拼接成激活码，并判断是否在pwds中，如果在则重新生成，否则添加到pwds中，继续循环\n",
    "3.打印pwds列表\n",
    "\n",
    "\n",
    "i = 0\n",
    "j = 0\n",
    "pwds = []\n",
    "while i < 200:\n",
    "    pwd = ''\n",
    "    while j < 10:\n",
    "        pwd = pwd+str(range(0,10))\n",
    "        j = j+1\n",
    "    if pwd not in pwds:\n",
    "        pwds.append(pwd)\n",
    "        i = i+1\n",
    "    else:\n",
    "        continue\n",
    "print(pwds)\n",
    "'''\n",
    "\n",
    "\n",
    "'''\n",
    "解法二：参考网上进行理解\n",
    "调用外部函数，进行字符串生成，并拼接起来\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1808f569",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\n",
      "9jUH\n",
      "tQoX—Xcy3—pjy6—uGIR\n",
      "f1eh—0kyK—zjda—DYqA\n",
      "iumH—h4u0—V5QI—u53x\n",
      "i85u—3o7S—VON7—mV9B\n",
      "1Fcu—L4YI—15ba—Mfvc\n",
      "L5CT—8qv2—RcqI—vMK1\n",
      "3tVz—oasG—Uv9t—KjiM\n",
      "clGR—8QV6—uiYp—DOfY\n",
      "5odL—BAZO—kD7h—d70q\n",
      "vjKc—cIsb—0dtv—vxSz\n",
      "OPaf—1xzv—PuHl—n9Qz\n",
      "2xMu—hMZJ—3BY1—skWJ\n",
      "pjts—MJPe—4RTz—jI8Z\n",
      "faOh—tr0K—rCxW—Xx5B\n",
      "ULJh—MSr0—vs2K—6Rez\n",
      "wWB0—rmQq—XAgk—YADZ\n",
      "T9fM—otE5—SHrE—fKrz\n",
      "Kia6—xI5n—paiX—lOP9\n",
      "xhKD—QJN0—eGya—zuvk\n",
      "Ps3B—jL68—FK7L—Dsqc\n",
      "T2ao—zRaN—KBrT—LAtw\n",
      "uBts—40ck—zwQN—8Bvd\n",
      "kXFx—xFGX—IYPx—3yMw\n",
      "0lTN—hIsY—8u51—Arbg\n",
      "GfmP—VhgM—Q95Y—aMSE\n",
      "Ke4h—1VlU—Su4g—iRth\n",
      "IJa2—cZuq—6z4W—okI0\n",
      "jhBa—AoP8—3DS2—qxmy\n",
      "Rdjr—vCPd—7Dc3—q53B\n",
      "s8IN—KYUX—uHSw—Sbfk\n",
      "ZRQP—GpdQ—RETl—jWOX\n",
      "3Qfi—xpLT—jUGR—3lNU\n",
      "Cfzd—qr0o—AfYR—tWwq\n",
      "SEzr—dxuI—ozdh—ZPlV\n",
      "6keT—uavK—SN3l—jr24\n",
      "T5sR—3Cwl—gKIr—cbnj\n",
      "lbgQ—MwNE—Lzvp—gV7Y\n",
      "cRZt—YmfN—ebSC—0ICF\n",
      "fxOL—Po9G—b54k—ru0K\n",
      "3j5r—0TCG—RjNl—H02c\n",
      "mzLT—z1lo—9gqR—bGL4\n",
      "K8hQ—iAJ5—OFE4—lxHK\n",
      "5NwO—sA4k—8Ow4—TEnr\n",
      "C2qS—9sHy—X3ws—Sgfm\n",
      "dzWq—K6Fo—0VdK—ZG14\n",
      "hY4n—OetT—36vD—9UFs\n",
      "gRNH—HlSX—1Xp4—mJft\n",
      "vU8V—MLYQ—65rO—1XuK\n",
      "niwL—5rVR—9jOr—I4UT\n",
      "fnaH—m9DU—ZM8T—xeiy\n",
      "g0F9—RZQ5—2SEo—2Xlt\n",
      "lvu8—A0Jj—amQ5—r0z3\n",
      "spqE—41T7—Guc6—Wz8Q\n",
      "3CTq—Wqfl—4Cl6—BNmD\n",
      "HEGy—BPR3—ANgY—Rzjs\n",
      "qgfw—52Yl—tI8b—SHX0\n",
      "YI2F—kOmp—aIr3—87sO\n",
      "dtSk—sgA5—ZbOx—Lw3T\n",
      "axTJ—ZQOo—D9fK—JxrK\n",
      "A96y—H0W6—vf2H—TDCV\n",
      "odbn—8FpE—4r0V—2JPU\n",
      "Vj4D—FegJ—U1jJ—GOUN\n",
      "d43g—nmJg—ktZQ—xgTt\n",
      "0X9Z—3PrU—VEHc—Ek2t\n",
      "zaLn—GWvT—RJW7—vZzn\n",
      "lBts—fAWl—Q6wq—ldBb\n",
      "QLhH—o1Xt—Ci82—1CAt\n",
      "C862—zAsS—Jn3C—PLQK\n",
      "KIqj—LEWV—JCbY—0Yq2\n",
      "RFd5—D1mE—NBiW—zXkl\n",
      "dO4k—9gN5—6x7P—o7YO\n",
      "JbAU—opC9—gCuw—ZJzm\n",
      "z0QW—rODY—TAfK—yvXu\n",
      "ekOB—HIpP—Fmv0—zSWT\n",
      "WBuP—2NKF—7Tkt—9wmi\n",
      "WMER—xvcS—7U9a—b9z1\n",
      "84we—9RQs—2bVs—8qIn\n",
      "5DRI—sNBT—9h0n—UXdL\n",
      "nzOg—HCUk—Iybk—cSXM\n",
      "rkRJ—bmN4—RZvG—xDqt\n",
      "GMBi—tj6g—VLAI—UPu8\n",
      "OmFD—GCgK—VusJ—dYqC\n",
      "1K0B—CdDh—Jjeg—90Tl\n",
      "sW2q—8Bfk—qfpe—cyNa\n",
      "RS6E—1ho9—IrTb—5DVI\n",
      "fCWd—97Zd—y2CS—wA4c\n",
      "JHXF—UV6L—AJWw—sqjw\n",
      "4k5g—HEI6—QXwL—0TUS\n",
      "NHfs—YEma—AdXp—c8Yf\n",
      "JjHt—tDQ1—USOf—rRuq\n",
      "OGdE—WNM2—KgPi—NCep\n",
      "OgxX—adzL—lyuq—dshm\n",
      "xL96—4Qvm—bD4f—u8dk\n",
      "o3R5—7QVs—8WDz—Ktx4\n",
      "sbA0—UfZY—jyRc—eaKN\n",
      "90Yv—cGO2—85We—YQft\n",
      "J0sE—Awlc—sznm—LQ7B\n",
      "qSlF—fARM—CuHI—jmMk\n",
      "pLxv—JNLC—EiNd—EbQq\n",
      "d8xW—HlFM—ZDTF—Njmc\n",
      "uCDN—usHg—H6xj—FwzZ\n",
      "DLVg—XNhW—ivxV—IE3S\n",
      "BKb9—A5Ns—Z5FX—740O\n",
      "j8wr—1Ulg—GRjm—qNCv\n",
      "Ak26—BEuw—BgUx—VhvY\n",
      "oyLY—zd7w—L05A—v674\n",
      "KzoP—dpCE—kuXe—lhbL\n",
      "kjMz—HlEa—fzAc—qfTU\n",
      "839W—ktpn—YeXN—KvAQ\n",
      "9TY4—06JS—BAbF—T9Hj\n",
      "RA42—fRIO—MXqU—5Pd1\n",
      "XwQJ—LwKg—9jW4—dtAU\n",
      "iSwd—8aeF—HRkh—3tDa\n",
      "KCJZ—5W3Z—4wH6—bi3a\n",
      "qlUX—2zBC—VdiG—LEbw\n",
      "l1Qa—58M4—ofM3—6kNq\n",
      "MdpR—0lJF—dXKf—37dr\n",
      "chyZ—Z3Fz—ep5b—WB6i\n",
      "QqvU—CHwl—ypzn—v9L5\n",
      "DuJQ—9e64—E2K1—7LY9\n",
      "AFiU—cg2o—nTFB—YqVe\n",
      "VpxB—N73M—xrEC—sVO1\n",
      "7XCp—8P6u—2SKp—F280\n",
      "q67W—q1U3—2z4k—OTWG\n",
      "IqRD—R1oW—h4xL—7O6L\n",
      "jz54—6col—oipI—m8pS\n",
      "5PGB—Myez—0EcO—NTQI\n",
      "oXnC—aJw3—uxwq—cTd9\n",
      "rAH8—9BGt—oguG—uwKI\n",
      "s4Hz—TIrB—QdO0—xzKQ\n",
      "BZf1—YgNm—GgUq—CVHt\n",
      "mWjb—zrlJ—RAne—YapF\n",
      "w9zC—9miV—JC6c—xpYO\n",
      "feN3—J10H—SY0M—KTXL\n",
      "KM2Y—z2vT—riPK—Ax2C\n",
      "C6xq—LE4G—mxHP—z9f8\n",
      "ZO9o—YwZ4—UV7h—Aj4H\n",
      "1Py4—fDRS—92Rd—cFSg\n",
      "DgvO—CNaP—rs6P—cOjd\n",
      "0uUV—Am8b—LsDj—xECk\n",
      "1UOg—jgvZ—keF1—b2SG\n",
      "bdw5—PAl9—eJ9o—HVsg\n",
      "8n5l—h2QK—6bWe—Vy9B\n",
      "MZxd—kstp—gVYx—2PWh\n",
      "PKLE—IFA0—eyJj—kGCy\n",
      "lfj1—0Mt4—9kAL—ZhOy\n",
      "5L7n—jbiH—rqoV—mYju\n",
      "dF4Q—4Ume—GAmd—aRi3\n",
      "FVzW—nXQH—GwaR—OSTV\n",
      "K1AJ—E2Xc—IcEN—OCNl\n",
      "1NHk—Q9W8—G1MH—GEQt\n",
      "2xsq—x5fW—8dBi—WJ4N\n",
      "dYpH—wRzd—5icr—4C7P\n",
      "9v4R—05dy—ZAJo—7Scy\n",
      "jURm—lgCw—VjWG—ZOCo\n",
      "rOal—SJlB—e9Ep—wgkB\n",
      "GXlr—gqA6—Qqmw—kdQT\n",
      "itIZ—Atvg—UNo3—qEC4\n",
      "IA4U—x5dD—EN1I—yaY8\n",
      "fgmG—NHIj—YMAB—ITGY\n",
      "FJlt—6wV8—CTcI—SV60\n",
      "fXEz—deZv—rgRO—OQUP\n",
      "aIiD—zxi6—dFtU—NbgR\n",
      "MnOR—CQJO—nCj7—nt81\n",
      "kMyA—qk1g—U6Sx—OKQ9\n",
      "9z5k—DZvz—ybFK—nX1K\n",
      "bNXw—AVym—EaUg—s1an\n",
      "raLc—WolP—REzg—jRXE\n",
      "MBrd—8CN7—KNt9—LYrV\n",
      "2Mbp—gZ6V—JUgV—w45h\n",
      "TNf2—1Gmj—rxyG—n2hx\n",
      "4eEH—eHS7—0nDG—IwnS\n",
      "WtJR—dbJL—T6Nf—PI9W\n",
      "VPfS—WozO—JfKV—C5uQ\n",
      "WSGg—YW2K—bHzN—r8qC\n",
      "Q4uY—zOja—hjfC—X2Wx\n",
      "BcMN—kE82—ihK3—LItw\n",
      "EwXz—GugX—OcX9—Z8FC\n",
      "8eAM—dtBy—Jy7a—vYfr\n",
      "eNLQ—ujAh—vJId—fMgb\n",
      "MlQX—2JgG—IKO1—Oa3o\n",
      "TZ6h—atEl—49vU—3tke\n",
      "Rhie—B62d—fFNb—MVLE\n",
      "CXvo—1ueK—HKIt—kjEI\n",
      "sCXk—X91m—DWjG—Uwa1\n",
      "tEfc—3Dol—XEF6—kNg5\n",
      "XbWT—cnQC—Sk1j—ch87\n",
      "f0vb—Afo5—an7N—D3iT\n",
      "RoOT—KtqA—yWow—mp1B\n",
      "ihPw—1M9P—5E7O—dcgw\n",
      "6SFn—JQd8—rsdW—BD9s\n",
      "tDzS—d6lO—1fXu—pyRQ\n",
      "u0Gs—qlSb—QqFI—KYEN\n",
      "ADnz—0j4Q—9Lis—IkDh\n",
      "4zit—0hOk—SH8Z—90bM\n",
      "O9Ui—0WZh—GwPA—JGvq\n",
      "NQW2—yYIW—TqZX—pmXT\n",
      "Vo74—6Kej—iMcV—mkZa\n",
      "srBH—sFhR—zdVq—lojf\n",
      "LqkS—mFSr—vbP9—gHdM\n",
      "M86l—XBWL—mtoJ—P239\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "import string\n",
    "\n",
    "\n",
    "code = string.ascii_letters + string.digits\n",
    "print(code)\n",
    "\n",
    "#获取随机生成的4位字符串\n",
    "def getCode():\n",
    "    return \"\".join(random.sample(code, 4))\n",
    "print(getCode())\n",
    "\n",
    "\n",
    "#获取4列4位激活码 以\"—\"串联起来\n",
    "def key(group):\n",
    "    return \"—\".join([getCode() for i in range(group)])\n",
    "print(key(4))\n",
    "\n",
    "\n",
    "#获取200组激活码  每组4列4位\n",
    "def keys(n):\n",
    "    return \"\\n\".join([key(4) for i in range(n)])\n",
    "\n",
    "print(keys(200))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a52df9e1",
   "metadata": {},
   "source": [
    "## 9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b33a25ba",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "解法：参考网上已理解\n",
    "编写一个特定函数并调用，函数中采用特定函数进行文件遍历，和查询特定后缀的文件\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "23a21ee3",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "ename": "PermissionError",
     "evalue": "[WinError 5] 拒绝访问。: 'C:\\\\/$Recycle.Bin/S-1-5-18'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mPermissionError\u001b[0m                           Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-13-1f79a781b8dd>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     25\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     26\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0m__name__\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m\"__main__\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 27\u001b[1;33m     \u001b[0msys\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmain\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-13-1f79a781b8dd>\u001b[0m in \u001b[0;36mmain\u001b[1;34m(argv)\u001b[0m\n\u001b[0;32m     20\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mmain\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0margv\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     21\u001b[0m     \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 22\u001b[1;33m     \u001b[0mlm_find_files\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"C:\\\\\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\".doc\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     23\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     24\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-13-1f79a781b8dd>\u001b[0m in \u001b[0;36mlm_find_files\u001b[1;34m(path, target, result)\u001b[0m\n\u001b[0;32m     14\u001b[0m                 \u001b[1;32mpass\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     15\u001b[0m             \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 16\u001b[1;33m                 \u001b[0mlm_find_files\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnpath\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtarget\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     17\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     18\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-13-1f79a781b8dd>\u001b[0m in \u001b[0;36mlm_find_files\u001b[1;34m(path, target, result)\u001b[0m\n\u001b[0;32m     14\u001b[0m                 \u001b[1;32mpass\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     15\u001b[0m             \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 16\u001b[1;33m                 \u001b[0mlm_find_files\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnpath\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtarget\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     17\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     18\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-13-1f79a781b8dd>\u001b[0m in \u001b[0;36mlm_find_files\u001b[1;34m(path, target, result)\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mlm_find_files\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtarget\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m     \u001b[0mfiles\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlistdir\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m;\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      7\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mf\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mfiles\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m         \u001b[0mnpath\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpath\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;34m'/'\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mPermissionError\u001b[0m: [WinError 5] 拒绝访问。: 'C:\\\\/$Recycle.Bin/S-1-5-18'"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "import os\n",
    "\n",
    "\n",
    "def lm_find_files(path, target, result):\n",
    "    files = os.listdir(path);\n",
    "    for f in files:\n",
    "        npath = path + '/' + f\n",
    "        if (os.path.isfile(npath)):\n",
    "            if (os.path.splitext(npath)[1] == target):\n",
    "                result.append(npath)\n",
    "        if (os.path.isdir(npath)):\n",
    "            if (f[0] == '.'):\n",
    "                pass\n",
    "            else:\n",
    "                lm_find_files(npath, target, result)\n",
    "    return result\n",
    "\n",
    "\n",
    "def main(argv=None):\n",
    "    result = []\n",
    "    lm_find_files(\"C:\\\\\", \".doc\", result)\n",
    "    print(result)\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    sys.exit(main())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3076455f",
   "metadata": {},
   "source": [
    "## 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da21dbfc",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "解法：参考网上已理解\n",
    "1.编写函数并调用：函数中首先定位目标路径，然后查找指定文件中的内容。\n",
    "2.根据正则表达式，判断代码行，空行，注释行，计数\n",
    "3.打印结果\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2898f5c9",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "code number is 27\n",
      "empty number is 5\n",
      "note number is 0\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import re\n",
    "\n",
    "os.chdir('E:\\\\3PythonProject\\\\1-PythonSE\\Homework')\n",
    "\n",
    "fh=open('1zifuchuan.py', 'r', encoding='utf-8')\n",
    "read_fh=fh.readlines()\n",
    "fh.close()\n",
    "number_code=0\n",
    "number_empty=0\n",
    "number_note=0\n",
    "pattern='.*#' #正则匹配模式\n",
    "\n",
    "for x in read_fh:\n",
    "    if '#' in x : #计算注释数目\n",
    "        if re.findall(pattern,x)[0][:-1].isspace() or re.findall(pattern,x)[0][:-1]=='':\n",
    "            number_note+=1\n",
    "        else:\n",
    "            number_code+=1\n",
    "\n",
    "    elif x.isspace():\n",
    "        number_empty+=1\n",
    "    else:\n",
    "        number_code+=1\n",
    "print ('code number is %d'%(number_code+number_empty+number_note))\n",
    "print ('empty number is %d'%number_empty)\n",
    "print ('note number is %d'%number_note)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
